<template>
    <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick">
        <el-tab-pane label="学生假条" name="student"></el-tab-pane>
        <el-tab-pane label="教师假条" name="teacher"></el-tab-pane>
    </el-tabs>
    <el-table v-loading="tableLoading" :data="tableData" style="width:70%">
        <el-table-column prop="ask_user" label="申请人" width="120" />
        <el-table-column prop="ask_time" label="申请时间" width="180" />
        <el-table-column prop="description" label="请假原因" width="180" />
        <el-table-column prop="start_time" label="开始时间" width="180" />
        <el-table-column prop="end_time" label="结束时间" width="180" />
        <el-table-column label="操作" width="180">
            <template #default="scope">
                <el-button type="success" @click="agree_leave(scope.row)">同意</el-button>
                <el-popconfirm title="是否拒绝？" @confirm="refuse_leave(scope.row.leave_id)">
                    <template #reference>
                        <el-button color="red">拒绝</el-button>
                    </template>
                </el-popconfirm>
            </template>
        </el-table-column>
    </el-table>
</template>

<script setup>
import { ref, onMounted } from "vue"
import axios from '../../utils/http.js'
import formatTime from '../../utils/formattime.js'
let tableData = ref([])
let tableLoading = ref(true)
let activeName = ref('student')
import { ElMessage } from 'element-plus'
const refuse_leave = (id) => {
    console.log(id);
    axios({
        url: 'leave/get_leave_list/',
        method: 'DELETE',
        data: {
            leave_id: id
        },
        params: {
            username: localStorage.getItem('username')
        }
    }).then(res => {
        if (res.data.code == 200) {
            update_list()
            ElMessage({
                message: '请假申请已拒绝',
                type: 'success'
            })
        } else {
            ElMessage({
                message: '审批假条失败',
                type: 'error'
            })
        }
    })
}
const agree_leave = (row) => {
    axios({
        url: 'leave/get_leave_list/',
        method: 'PUT',
        params: {
            username: localStorage.getItem('username')
        },
        data: {
            leave_id: row.leave_id
        }
    }).then(res => {
        if (res.data.code == 200) {
            update_list()
            ElMessage({
                message: '请假申请已通过',
                type: 'success'
            })
        } else {
            ElMessage({
                message: '审批假条失败',
                type: 'error'
            })
        }
    })
}
const handleClick = (tab, event) => {
    tableLoading.value = true
    tableData.value = []
    if (tab.props.name=='student'){
        update_list()
    }else{
        get_teacher_leavelist()
    }
    
}

onMounted(() => {
    update_list()
})
function update_list() {
    axios({
        url: 'leave/get_leave_list/',
        method: 'GET',
        params: {
            username: localStorage.getItem('username')
        }
    }).then(res => {
        if (res.data.code == 200) {
            tableLoading.value = false
            tableData.value = res.data.leave_list
            for (var i = 0; i < tableData.value.length; i++) {
                tableData.value[i].ask_time = formatTime(tableData.value[i].ask_time)
                tableData.value[i].start_time = formatTime(tableData.value[i].start_time)
                tableData.value[i].end_time = formatTime(tableData.value[i].end_time)
            }
        }
    })
}
function get_teacher_leavelist() {
    axios({
        url: 'leave/teacher_leave/',
        method: 'GET'
    }).then(res => {
        if (res.data.code == 201) {
            tableLoading.value = false
            tableData.value = res.data.data
            console.log(tableData.value);
            for (var i = 0; i < tableData.value.length; i++) {
                tableData.value[i].ask_time = formatTime(tableData.value[i].ask_time)
                tableData.value[i].start_time = formatTime(tableData.value[i].start_time)
                tableData.value[i].end_time = formatTime(tableData.value[i].end_time)
            }
        }
    })
}
</script>

<style scoped></style>